package com.dmgis.qxfw.dataservice.businessservice.yyjt.controller;

import com.dmgis.entity.DmMapPoint;
import com.dmgis.qxfw.dataservice.businessservice.api.domain.DbConfig;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.adminCode;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.raidentemp;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.qx.ybzz_forecastfinal;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.thunder_fcst;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.traffic;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.traffic_hightway_pnt;
import com.dmgis.qxfw.dataservice.businessservice.api.entity.zy.traffic_weather_sta;
import com.dmgis.qxfw.dataservice.businessservice.common.domain.CityFcstInfo;
import com.dmgis.qxfw.dataservice.businessservice.common.domain.MyPoint;
import com.dmgis.qxfw.dataservice.businessservice.common.domain.TrafficColor;
import com.dmgis.qxfw.dataservice.businessservice.common.domain.TrafficSearch;
import com.dmgis.qxfw.dataservice.businessservice.common.entity.*;
import com.dmgis.qxfw.dataservice.businessservice.common.service.IConfigService;
import com.dmgis.qxfw.dataservice.businessservice.common.util.*;
import com.dmgis.qxfw.dataservice.businessservice.dmgis_mongodb_srv.dataservice.service.Impl.MongoService1;
import com.dmgis.qxfw.dataservice.businessservice.qxinfo.service.IQxInfoService;
import com.dmgis.qxfw.dataservice.businessservice.yyjt.service.IForecastInfoService;

import com.dmgis.qxfw.dataservice.gisservice.service.ITransformService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/*气象信息预报*/
@Controller
@RequestMapping("/xtjt")
public class ForecastInfoController {
    private  final String operatingSystem;

    @Autowired
    public ForecastInfoController(String operatingSystem) {
        this.operatingSystem = operatingSystem;
    }
    @Resource
    private IForecastInfoService forecastInfoService;
    @Resource
    private IConfigService configService;
    @Resource
    private IQxInfoService qxInfoService;
    @Resource
    private ITransformService transformService;

    /* 日志记录 */
    private static Logger log = Logger.getLogger(ForecastInfoController.class);

    MongoService1 ms = new MongoService1();


    /* 初始化高速公路界面 */
    @RequestMapping("/initHighwayPage")
    @ResponseBody
    public Object initHighwayPage() {
        ArrayList<Object> arrayList = new ArrayList<Object>();/* object各种类型数据 */
        TownshipForecastKey newestDate = forecastInfoService.getTownNewDate();
        // 加载列表
        String timechar = newestDate.getTimeChar();
        String datechar = new DateTime(newestDate.getDateChar()).toDateString();// 将日期转换为字符串格式
        List<traffic_hightway_pnt> pnt = forecastInfoService.getHightwaypnt();
        arrayList.add(timechar);
        arrayList.add(datechar);
        arrayList.add(pnt);
        return arrayList;
    }

    /* 初始化高速公路雷电界面 */
    @RequestMapping("/initHighwayLDPage")
    @ResponseBody
    public Object initHighwayLDPage() {
        ArrayList<Object> arrayList = new ArrayList<Object>();/* object各种类型数据 */
        thunder_fcst newestDate = qxInfoService.getLDNewDate();
        // 加载列表
        String timechar = newestDate.getForecasttime().toString();
        String datechar = new DateTime(newestDate.getReporttime()).toDateString();// 将日期转换为字符串格式
        arrayList.add(timechar);
        arrayList.add(datechar);
        return arrayList;
    }

    /* 高速公路预报 */
    @RequestMapping("/getHighway")
    @ResponseBody
    public Object getHighway(HttpServletRequest request, Model model) throws Exception {
        TownshipForecastKey selectKey = new TownshipForecastKey();// 查询类
        String time = request.getParameter("date");// 时间
        String sc = request.getParameter("sc");// 时次
        String sx = request.getParameter("sx");// 时效
        String staName = request.getParameter("staId");//预报站点编号
        String stationName = request.getParameter("staName");//
        String strCountyName = "";
        int nTimes;
        List<CityFcstInfo> resultList = new ArrayList<CityFcstInfo>();
        WebCtrlData wcd = new WebCtrlData();
        try {
            nTimes = Integer.parseInt(sx);
            selectKey.setNTimes(nTimes);// 时效
            selectKey.setTimeChar(sc);// 时次
            selectKey.setDateChar(DateTime.parseDate(time).toDate());// 时间
            selectKey.setStationID(staName);
            selectKey.setStationName(stationName);
            List<TownshipForecast> fcstList = forecastInfoService.getHighway(selectKey);// 获取站点编号、名称、天气现象
            int count = fcstList.toArray().length;
            for (int i = 0; i < count; i += 2) {
                strCountyName = fcstList.get(i).getStationName();
                String pic1 = String.format("%02d",
                        Integer.parseInt(WebCtrlData.WeatherNameToPicPath(fcstList.get(i).getWeather1())));
                String pic2 = String.format("%02d",
                        Integer.parseInt(WebCtrlData.WeatherNameToPicPath(fcstList.get(i + 1).getWeather1())));
                String weather = "";// 天气信息
                String windD = "";// 风向信息
                String windV = "";// 风速信息
                String windS = "";// 风速信息
                String windD12 = wcd.GetAllFx(fcstList.get(i).getWindD().toString().replace("-65535", "0").trim());
                String windD24 = wcd.GetAllFx(fcstList.get(i + 1).getWindD().toString().replace("-65535", "0").trim());
                String windV12 =  wcd.GetWeatherFs(String.valueOf(WebCtrlData.FengSuToFengJi(fcstList.get(i).getWindS())));
                String windV24 =  wcd.GetWeatherFs(String.valueOf(WebCtrlData.FengSuToFengJi(fcstList.get(i + 1).getWindS())));
                String windS12 = String.valueOf(fcstList.get(i).getWindS());
                String windS24 = String.valueOf(fcstList.get(i + 1).getWindS());
                if (windD12.equals(windD24)) {
                    windD += windD12;
                } else {
                    windD += windD12 + "转" + windD24;
                }
                if (windV12.equals(windV24)) {
                    windV += windV12 ;
                } else {
                    windV += windV12 + "~" + windV24;
                }
                if (windS12.equals(windS24)) {
                    windS += windS12 + "m/s";
                } else {
                    windS += windS12 + "~" + windS24 + "m/s";
                }
                if (fcstList.get(i).getWeather1().trim().equals(fcstList.get(i + 1).getWeather1().trim())) {
                    weather += fcstList.get(i).getWeather1().trim();
                } else {
                    weather += fcstList.get(i).getWeather1().trim() + "转" + fcstList.get(i + 1).getWeather1().trim();
                }
                double maxTemp = fcstList.get(i).getMaxTemp() > fcstList.get(i + 1).getMaxTemp()
                        ? fcstList.get(i).getMaxTemp() : fcstList.get(i + 1).getMaxTemp();
                double minTemp = fcstList.get(i).getMinTemp() > fcstList.get(i + 1).getMinTemp()
                        ? fcstList.get(i + 1).getMinTemp() : fcstList.get(i).getMinTemp();
                CityFcstInfo fcstInfo = new CityFcstInfo();
                fcstInfo.setPic1(pic1);
                fcstInfo.setPic2(pic2);
                fcstInfo.setStationName(strCountyName);
                fcstInfo.setStationType(fcstList.get(i).getStationtype());
                fcstInfo.setLat(fcstList.get(i).getLatitude());
                fcstInfo.setLon(fcstList.get(i).getLongitude());
                fcstInfo.setMinTemp(minTemp);
                fcstInfo.setMaxTemp(maxTemp);
                DmMapPoint dmDot = new DmMapPoint(fcstList.get(i).getLongitude(), fcstList.get(i).getLatitude());
                DmMapPoint ltDot = transformService.LonLatConvertToLambert(dmDot.x, dmDot.y);
                fcstInfo.setY(ltDot.y);
                fcstInfo.setX(ltDot.x);
                fcstInfo.setWindD(windD);
                fcstInfo.setWindV(windV);
                fcstInfo.setWindS(windS);
                fcstInfo.setWeather1(fcstList.get(i).getWeather1().trim());
                fcstInfo.setWeather2(fcstList.get(i + 1).getWeather1().trim());
                fcstInfo.setWeather(weather);
                resultList.add(fcstInfo);
            }
        } catch (Exception e) {
            // TODO: 高速公路查询异常
            log.error(e);
        }
        return resultList;
    }

    /* 高速公路预报 */
    @RequestMapping("/getDayHighway")
    @ResponseBody
    public Object getDayHighway(HttpServletRequest request, Model model) throws Exception {
        TownshipForecastKey selectKey = new TownshipForecastKey();// 查询类
        String time = request.getParameter("date");// 时间
        String staName = request.getParameter("staId");//预报站点编号
        String sc = request.getParameter("sc");// 时次
        String type = request.getParameter("type");//类型
        String stationName = request.getParameter("staName");//类型
        String strCountyName = "";
        List<CityFcstInfo> resultList = new ArrayList<CityFcstInfo>();
        WebCtrlData wcd = new WebCtrlData();
        try {
            selectKey.setDateChar(DateTime.parseDate(time).toDate());// 时间
            selectKey.setStationID(staName);
            selectKey.setStationName(stationName);
            selectKey.setTimeChar(sc);
            if (type.equals("24")) {
                List<TownshipForecast> fcstList = forecastInfoService.getHighway(selectKey);// 获取站点编号、名称、天气现象
                int count = fcstList.toArray().length;
                int j = 1;
                for (int i = 0; i < count; i += 2) {
                    strCountyName = fcstList.get(i).getStationName();
                    String pic1 = String.format("%02d",
                            Integer.parseInt(WebCtrlData.WeatherNameToPicPath(fcstList.get(i).getWeather1())));
                    String pic2 = String.format("%02d",
                            Integer.parseInt(WebCtrlData.WeatherNameToPicPath(fcstList.get(i + 1).getWeather1())));
                    String weather = "";// 天气信息
                    String windD = "";// 风向信息
                    String windV = "";// 风速信息
                    String windS = "";// 风速信息
                    String windD12 = wcd.GetAllFx(fcstList.get(i).getWindD().toString().replace("-65535", "0").trim());
                    String windD24 = wcd.GetAllFx(fcstList.get(i + 1).getWindD().toString().replace("-65535", "0").trim());
                    String windV12 =  wcd.GetWeatherFs(String.valueOf(WebCtrlData.FengSuToFengJi(fcstList.get(i).getWindS())));
                    String windV24 =  wcd.GetWeatherFs(String.valueOf(WebCtrlData.FengSuToFengJi(fcstList.get(i + 1).getWindS())));
                    String windS12 = String.valueOf(fcstList.get(i).getWindS());
                    String windS24 = String.valueOf(fcstList.get(i + 1).getWindS());
                    if (windD12.equals(windD24)) {
                        windD += windD12;
                    } else {
                        windD += windD12 + "转" + windD24;
                    }
                    if (windV12.equals(windV24)) {
                        windV += windV12 ;
                    } else {
                        windV += windV12 + "~" + windV24 ;
                    }
                    if (windS12.equals(windS24)) {
                        windS += windS12 + "m/s";
                    } else {
                        windS += windS12 + "~" + windS24 + "m/s";
                    }
                    if (fcstList.get(i).getWeather1().trim().equals(fcstList.get(i + 1).getWeather1().trim())) {
                        weather += fcstList.get(i).getWeather1().trim();
                    } else {
                        weather += fcstList.get(i).getWeather1().trim() + "转" + fcstList.get(i + 1).getWeather1().trim();
                    }
                    double maxTemp = fcstList.get(i).getMaxTemp() > fcstList.get(i + 1).getMaxTemp()
                            ? fcstList.get(i).getMaxTemp() : fcstList.get(i + 1).getMaxTemp();
                    double minTemp = fcstList.get(i).getMinTemp() > fcstList.get(i + 1).getMinTemp()
                            ? fcstList.get(i + 1).getMinTemp() : fcstList.get(i).getMinTemp();
                    CityFcstInfo fcstInfo = new CityFcstInfo();
                    fcstInfo.setPic1(pic1);
                    fcstInfo.setPic2(pic2);
                    fcstInfo.setStationName(strCountyName);
                    fcstInfo.setStationType(fcstList.get(i).getStationtype());
                    fcstInfo.setLat(fcstList.get(i).getLatitude());
                    fcstInfo.setLon(fcstList.get(i).getLongitude());
                    fcstInfo.setMinTemp(minTemp);
                    fcstInfo.setMaxTemp(maxTemp);
                    DmMapPoint Dt = new DmMapPoint(fcstList.get(i).getLongitude(), fcstList.get(i).getLatitude());
                    DmMapPoint ltDot = transformService.LonLatConvertToLambert(Dt.x, Dt.y);
                    fcstInfo.setY(ltDot.y);
                    fcstInfo.setX(ltDot.x);
                    fcstInfo.setWindD(windD);
                    fcstInfo.setWindV(windV);
                    fcstInfo.setWindS(windS);
                    fcstInfo.setWeather1(fcstList.get(i).getWeather1().trim());
                    fcstInfo.setWeather2(fcstList.get(i + 1).getWeather1().trim());
                    fcstInfo.setWeather(weather);
                    DateTime date = new DateTime(fcstList.get(i).getDateChar());

                    date = date.AddDays(j);
                    j++;
                    fcstInfo.setTime(date.toDateString());
                    resultList.add(fcstInfo);
                }
            } else if (type.equals("1")) {
                List<ybzz_forecastfinal> fcstList = forecastInfoService.getDayHighway(selectKey);// 获取站点编号、名称、天气现象
                if (fcstList != null && fcstList.size() > 0) {
                    for (ybzz_forecastfinal ybzz : fcstList) {
                        CityFcstInfo fcstInfo = new CityFcstInfo();
                        fcstInfo.setPic1(String.format("%02d", Integer.parseInt(WebCtrlData.WeatherNameToPicPath(ybzz.getWeather()))));
                        fcstInfo.setStationName(ybzz.getUsername());
                        if (ybzz.getMintemp() != null) {
                            fcstInfo.setMinTemp(ybzz.getMintemp());
                        }
                        if (ybzz.getMaxtemp() != null) {
                            fcstInfo.setMaxTemp(ybzz.getMaxtemp());
                        }
                        fcstInfo.setWindD(wcd.GetAllFx(ybzz.getWindd().toString().replace("-65535", "0").trim()));
                        fcstInfo.setWindV(wcd.GetWeatherFs(String.valueOf(WebCtrlData.FengSuToFengJi(ybzz.getWind()))));
                        fcstInfo.setWindS(ybzz.getWind().toString());
                        fcstInfo.setWeather(ybzz.getWeather());
                        fcstInfo.setTime(ybzz.getWeather12());
                        fcstInfo.setLon(ybzz.getLon());
                        fcstInfo.setLat(ybzz.getLat());
                        resultList.add(fcstInfo);
                    }
                }
            }
        } catch (Exception e) {
            // TODO: 高速公路查询异常
            log.error(e);
        }
        return resultList;
    }

    /* 高速公路雷电查询 */
    @RequestMapping("/ThunderFcst")
    @ResponseBody
    public Object ThunderFcst(HttpServletRequest request, Model model) throws Exception {
        String time = request.getParameter("date");// 时间
        String sc = request.getParameter("sc");// 时次
        String sx = request.getParameter("sx");// 时效
        List<thunder_fcst> resultList = qxInfoService.getThunderFcst(time, sc, sx);// 查询潜势预报
        List<thunder_fcst> resultfcst = new ArrayList<thunder_fcst>();
        List<traffic_hightway_pnt> result = forecastInfoService.getHightwaypnt();
        for (thunder_fcst thunder : resultList) {
            for (traffic_hightway_pnt pnt : result) {
                if (pnt.getRelfcststa().equals(thunder.getStationid())) {
                    String ldLevel = thunder.getPredictlevel().toString().trim();
                    String imageName = "";
                    Double dLightning = 0.0;// 雷电强度
                    switch (ldLevel) {
                        case "强":
                            imageName = "LevelFour";
                            dLightning = (double) 4;
                            break;
                        case "中":
                            imageName = "LevelThree";
                            dLightning = (double) 3;
                            break;
                        case "弱":
                            imageName = "LevelTwo";
                            dLightning = (double) 2;
                            break;
                        case "无":
                            imageName = "LevelOne";
                            dLightning = (double) 1;
                            break;
                    }
                    thunder.setImagename(imageName);
                    thunder.setDlightning(dLightning);
                    thunder_fcst fst = new thunder_fcst();
                    fst = thunder;
                    resultfcst.add(fst);
                }
            }
        }
        return resultfcst;//
    }


    /* 初始化交通界面 */
    @RequestMapping("/initTrafficPage")
    @ResponseBody
    public Object initTrafficPage() {
        ArrayList<Object> arrayList = new ArrayList<Object>();/* object各种类型数据 */
        Traffic newestDate = forecastInfoService.getTrafficNewDate();
        DateTime announcedTime = new DateTime(newestDate.getAnnouncedTime());// DateTime类型
        String announcedtime = announcedTime.toDateTimeString();// 最新时间
        arrayList.add(announcedtime);// 最新时间
        return arrayList;
    }


    /* 交通预报查询 */
    @RequestMapping("/getTraffic")
    @ResponseBody
    public Object getTraffic(HttpServletRequest request, Model model) {
        TrafficSearch selectKey = new TrafficSearch();// 查询类
        String time = request.getParameter("date");// 获取开始时间
        String types = request.getParameter("type");
        String ntimes = request.getParameter("ntimes");
        String forcastlevel = request.getParameter("forcastlevel");
        String staName = request.getParameter("staName");
        if (StringUtil.IsNullOrEmpty(ntimes)) {
            if (forcastlevel.equals("24")) {
                ntimes = "24,48,72,96,120,144,168";
            } else {
                for (int i = 1; i < 25; i++) {
                    ntimes += i + ",";
                }
                if (ntimes.endsWith(",")) {
                    ntimes = ntimes.substring(0, ntimes.length() - 1);
                }
            }
        }
        selectKey.setTime(time);// 保存在selectKey中
        selectKey.setTypes(types);
        selectKey.setNtimes(ntimes);
        selectKey.setForcastlevel(forcastlevel);
        selectKey.setStrstaName(staName);
        List<Object> result = new ArrayList<Object>();
        List<Traffic> resultList = forecastInfoService.getTraffic(selectKey);
        for (Traffic tra : resultList) {
            DmMapPoint dmDot = new DmMapPoint(tra.getLon(), tra.getLat());
            DmMapPoint ltDot = transformService.LonLatConvertToLambert(dmDot.x, dmDot.y);
            tra.setX(ltDot.x);
            tra.setY(ltDot.y);
        }
        List<TrafficColor> tracolor = new ArrayList<TrafficColor>();
        TrafficColor color1 = new TrafficColor();
        color1.setElementColor(65280);
        color1.setElementValue("一级");
        TrafficColor color2 = new TrafficColor();
        color2.setElementColor(65535);
        color2.setElementValue("二级");
        TrafficColor color3 = new TrafficColor();
        color3.setElementColor(12171775);
        color3.setElementValue("三级");
        TrafficColor color4 = new TrafficColor();
        color4.setElementColor(3947775);
        color4.setElementValue("四级");
        TrafficColor color5 = new TrafficColor();
        color5.setElementColor(191);
        color5.setElementValue("五级");
        tracolor.add(color1);
        tracolor.add(color2);
        tracolor.add(color3);
        tracolor.add(color4);
        tracolor.add(color5);
        result.add(resultList);
        result.add(tracolor);
        return result;
    }

    /* 交通预报查询 */
    @RequestMapping("/getJXHTraffic")
    @ResponseBody
    public Object getJXHTraffic(HttpServletRequest request, Model model) {
        String startDate = request.getParameter("startDate") + ":00:00";// 时间
        String endDate = request.getParameter("endDate") + ":00:00";// 时次
        String types = request.getParameter("type");
        String forcastlevel = request.getParameter("forcastlevel");
        List<Object> result = new ArrayList<Object>();
        List<Traffic> resultList = forecastInfoService.getJXHTraffic(startDate, endDate, types, forcastlevel);
        for (Traffic tra : resultList) {
            DmMapPoint dmDot = new DmMapPoint(tra.getLon(), tra.getLat());
            DmMapPoint ltDot = transformService.LonLatConvertToLambert(dmDot.x, dmDot.y);
            tra.setX(ltDot.x);
            tra.setY(ltDot.y);
        }
        List<TrafficColor> tracolor = new ArrayList<TrafficColor>();
        TrafficColor color1 = new TrafficColor();
        color1.setElementColor(65280);
        color1.setElementValue("一级");
        TrafficColor color2 = new TrafficColor();
        color2.setElementColor(65535);
        color2.setElementValue("二级");
        TrafficColor color3 = new TrafficColor();
        color3.setElementColor(12171775);
        color3.setElementValue("三级");
        TrafficColor color4 = new TrafficColor();
        color4.setElementColor(3947775);
        color4.setElementValue("四级");
        TrafficColor color5 = new TrafficColor();
        color5.setElementColor(191);
        color5.setElementValue("五级");
        tracolor.add(color1);
        tracolor.add(color2);
        tracolor.add(color3);
        tracolor.add(color4);
        tracolor.add(color5);
        result.add(resultList);
        result.add(tracolor);
        return result;
    }


    /* 路段坐标 */
    @RequestMapping("/getCoordinate")
    @ResponseBody
    public Object getCoordinate(HttpServletRequest request, Model model) throws Exception {
        TrafficSearch selectKey = new TrafficSearch();// 查询类
        String types = request.getParameter("type");
        String time = request.getParameter("date");// 获取开始时间
        String ntimes = request.getParameter("ntimes");
        String forcastlevel = request.getParameter("forcastlevel");
        selectKey.setTime(time);// 保存在selectKey中
        selectKey.setTypes(types);
        selectKey.setNtimes(ntimes);
        selectKey.setForcastlevel(forcastlevel);
        List<Object> resultend = new ArrayList<Object>();

        List<Traffic> traffic = forecastInfoService.getTraffic(selectKey);
        List<TrafficColor> tracolor = new ArrayList<TrafficColor>();
        TrafficColor tracolors = new TrafficColor();
        List<Combatpoint> resultList = new ArrayList<Combatpoint>();
        List<ArrayList<MyPoint>> list = new ArrayList<ArrayList<MyPoint>>();
        String strstaNames = "";
        for (Traffic traffici : traffic) {
            Integer level = traffici.getForcastLevel();
            strstaNames = traffici.getStationName();
            if (level == 1) {
                tracolors.setElementValue("一级");
                tracolors.setElementColor(65280);
            }
            if (level == 2) {
                tracolors.setElementValue("二级");
                tracolors.setElementColor(65535);
            }
            if (level == 3) {
                tracolors.setElementValue("三级");
                tracolors.setElementColor(12171775);
            }
            if (level == 4) {
                tracolors.setElementValue("四级");
                tracolors.setElementColor(3947775);
            }
            if (level == 5) {
                tracolors.setElementValue("五级");
                tracolors.setElementColor(191);
            }
            tracolor.add(tracolors);
            selectKey.setStrstaName(strstaNames);

            resultList = forecastInfoService.getCoordinate(selectKey);
            String[] coordinate;
            ArrayList<MyPoint> tempPos = new ArrayList<MyPoint>();
            for (Combatpoint combatpoint : resultList) {
                coordinate = new String[combatpoint.getRoadcoordinate().split(",").length];
                coordinate = combatpoint.getRoadcoordinate().split(",");
                for (int i = 0; i < coordinate.length; i += 2) {
                    MyPoint point = new MyPoint(Double.parseDouble(coordinate[i]), Double.parseDouble(coordinate[i + 1]));
                    tempPos.add(point);
                }
                list.add(tempPos);
            }
        }
        resultend.add(list);
        resultend.add(tracolor);
        return resultend;
    }

    /* 得到交通预报文档 */
    @RequestMapping("/getTrafficDoc")
    @ResponseBody
    public Object getTrafficDoc(HttpServletRequest request, Model model) throws ParseException {
        TrafficSearch selectKey = new TrafficSearch();// 查询类
        String time = request.getParameter("date") + ":00:00";// 获取开始时间
        selectKey.setTime(time);// 保存在selectKey
        List<TrafficWord> resultList = forecastInfoService.getTrafficDoc(selectKey);

        String servicePath = "";// 服务器文件

        String docid = null;
        String docname = null;
        if (resultList != null && resultList.size() > 0) {
            docid = resultList.get(0).getId().toString();
            docname = resultList.get(0).getProductname();
        } else {
            System.err.println(time + "(此时间无预报数据)");
            return null;
        }
        String urlpath ;
        if (operatingSystem.equals("Windows")){
             urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp\\Traffic\\";// 临时文件目录
        }else {
             urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp\\Traffic\\";// 临时文件目录
        }

        FileUtil.mkDirs(urlpath);// 创建文件夹，生成多级目录
        FileUtil.ClearTempFiles(urlpath, 1000);// 清除缓存

        String fileName = docname;// 文件名
        String doctype = fileName.split("\\.")[fileName.split("\\.").length - 1];// 文件类型
        if (doctype.contains("doc")) {
            String pdfPath = urlpath + fileName.replace(doctype, "pdf");
            File pdfFile = new File(pdfPath);
            if (pdfFile.exists()) {
                if (operatingSystem.equals("Windows")){
                    servicePath = "Temp\\Traffic\\" + fileName.replace(doctype, "pdf");
                }else {
                    servicePath = "Temp/Traffic/" + fileName.replace(doctype, "pdf");
                }

            } else {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("id", docid);
                map.put("table_name", "base_product");
                map.put("collectionName", "pdf_file");
                map.put("advCode", DbConfig.advCode);
                byte[] data = ms.selectData(map);
                // 下载doc文件
                if (data == null) {
                    return "";
                }
                FileUtil.bytesToFile(data, pdfPath);
                if (operatingSystem.equals("Windows")){
                    servicePath = "Temp\\Traffic\\" + fileName.replace(doctype, "pdf");
                }else {
                    servicePath = "Temp/Traffic/" + fileName.replace(doctype, "pdf");
                }

            }
        }
        servicePath = servicePath.replace("\\", "/");
        return servicePath;
    }

    /* 得到交通预报文档 */
    @RequestMapping("/initTraffic")
    @ResponseBody
    public Object initTraffic() {
        ArrayList<Object> arrayList = new ArrayList<Object>();/* object各种类型数据 */
        Traffic newestDate = forecastInfoService.getNewDate();
        DateTime announcedTime = new DateTime(newestDate.getAnnouncedTime());// DateTime类型
        String announcedtime = announcedTime.toDateTimeString();// 最新时间
        arrayList.add(announcedtime);// 最新时间
        return arrayList;
    }


    /* 初始化交通界面 */
    @RequestMapping("/initJXHTraffic")
    @ResponseBody
    public Object initJXHTraffic(HttpServletRequest request, Model model) {
        String types = request.getParameter("type");
        List<traffic_weather_sta> arrayList = forecastInfoService.initJXHTraffic(types);
        return arrayList;
    }



    /* 初始化交通时次 */
    @RequestMapping("/initshici")
    @ResponseBody
    public Object initshici(HttpServletRequest request, Model model) {
        String sj = request.getParameter("sj");
        ArrayList<Object> arrayList = new ArrayList<Object>();/* object各种类型数据 */
        Traffic newestDate = forecastInfoService.getTrafficNewDate();
        DateTime announcedTime = new DateTime(newestDate.getAnnouncedTime());// DateTime类型
        String announcedtime = announcedTime.toDateTimeString();// 最新时间
        List<traffic> arrayList1 = forecastInfoService.initshici(announcedtime);
        return arrayList1;
    }



    @RequestMapping("/init")
    @ResponseBody
    public List<Object> init(){
        ArrayList<Object> arrayList = new ArrayList<Object>();
        ArrayList<String> nameList = new ArrayList<>();
        raidentemp newestDate = qxInfoService.getLDDate();
        // 加载列表
        DateTime dateTime = new DateTime(newestDate.getObservetime());
        String startTime = dateTime.AddHours(-1).toDateTimeString();
        String endTime = dateTime.toDateTimeString();
        //获取县级名称
        List<adminCode> adminCodeLs = configService.getTown();
        for (adminCode adminCodeL : adminCodeLs) {
            nameList.add(adminCodeL.getAdvname());
        }
        startTime="2024-06-11 03:06:59";
        endTime="2024-06-13 03:06:59";
        arrayList.add(startTime);
        arrayList.add(endTime);
        arrayList.add(nameList);
        return arrayList;
    }

    @RequestMapping("/getLddoc")
    @ResponseBody
    public Object getLddoc(HttpServletRequest request) {
        String kssj = request.getParameter("kssj");
        String jssj = request.getParameter("jssj");
        String type = request.getParameter("type");
        return forecastInfoService.getServiceDoc(type, kssj,jssj);// 查询时间段内的预警信息号
    }




    @RequestMapping("/showDoc")
    @ResponseBody
    public String showDoc(HttpServletRequest request, Model model) {
        String servicePath = "";
        String docId = request.getParameter("docId");// 文档编号
        String fileName = request.getParameter("docName");// 文档名称
        String tableName = request.getParameter("tableName");
        String urlpath ;
        if (operatingSystem.equals("Windows")){
             urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp\\ldwarning\\";
        }else {
             urlpath = request.getSession().getServletContext().getRealPath("/") + "Temp/ldwarning/";
        }

        String path = request.getContextPath();
        String basePath = DbConfig.IP + path;
        FileUtil.mkDirs(urlpath);// 创建文件夹
        // 当文件达到100张时清除
        FileUtil.ClearTempFiles(urlpath, 100);
        String docType = fileName.split("\\.")[fileName.split("\\.").length - 1];
        // 判断文件类型是否是word
        if (docType.contains("doc")) {
            String pdfPath = urlpath + fileName.replace(docType, "pdf");
            File pdfFile = new File(pdfPath);
            if (pdfFile.exists()) {
                if (operatingSystem.equals("Windows")){
                    servicePath = "Temp\\ldwarning\\" + fileName.replace(docType, "pdf");
                }else {
                    servicePath = "Temp/ldwarning/" + fileName.replace(docType, "pdf");
                }

                servicePath = basePath+servicePath;
            } else {
                // 从mongdo中获取文件
                Map<String, Object> map = new HashMap<String, Object>();
                // map.put("dateyear", String.valueOf(doppler.getDataYear()));
                map.put("id", docId);
                map.put("table_name", tableName);
                map.put("collectionName", "pdf_file");
                map.put("advCode", DbConfig.advCode);
                byte[] data = ms.selectData(map);
                FileUtil.bytesToFile(data, pdfPath);
                // 下载doc文件
                if (data == null) {
                    return "";
                }
                if (operatingSystem.equals("Windows")){
                    servicePath = "Temp\\ldwarning\\" + fileName.replace(docType, "pdf");
                }else {
                    servicePath = "Temp/ldwarning/" + fileName.replace(docType, "pdf");
                }

                servicePath = basePath+servicePath;
            }
        }

        servicePath = servicePath.replace("\\", "/");
        return servicePath;
    }

    @RequestMapping("LD")
    @ResponseBody
    public String ld(HttpServletRequest request){
        String kssj = request.getParameter("kssj").split(" ")[0]+" 00:00:00";
        String jssj = request.getParameter("jssj").split(" ")[0]+" 23:59:59";
        String stationName = request.getParameter("stationName");
        String result = "[";
        int count = 0;
        try {
            List<raidentemp> rList = new ArrayList<raidentemp>();
            rList = forecastInfoService.getLeidian(kssj, jssj, stationName,"0","100");
            if(rList!=null&&rList.size()>0){
                count = rList.size();
                for(raidentemp r : rList){
                    String row = "{";
                    row += "\"datetime\":\"" + new DateTime(r.getObservetime()).toDateTimeString() + "\",";
                    row += "\"lon\":\"" + r.getLon() + "\",";
                    row += "\"lat\":\"" + r.getLat() + "\",";
                    row += "\"current\":\"" + r.getStrength() + "\",";
                    row += "\"gradient\":\"" + r.getSteepness() + "\",";
                    row += "\"deviation\":\"" + r.getError() + "\"";
                    row = row + "},";
                    result += row;
                }
            }
            if(result.trim().endsWith(",")){
                result = result.trim().substring(0, result.trim().length()-1)+"]";
            }else{
                result ="[]";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }



}
